package com.ext.aic.box;

import android.content.Context;
import android.graphics.Rect;

import com.ext.aic.AICApplication;
import com.ext.aic.box.model.ClientUIConstants;
import com.ext.aic.box.model.PositionMeta;
import com.eyinfo.android_pure_utils.utils.PixelUtils;
import com.gyf.immersionbar.ImmersionBar;

public class PositionManager {
    private static volatile PositionManager instance;

    public static PositionManager getInstance() {
        if (instance == null) {
            synchronized (PositionManager.class) {
                if (instance == null) {
                    instance = new PositionManager();
                }
            }
        }
        return instance;
    }

    //获取聊天编辑器位置
    public PositionMeta getChatEditPosition(ClientUIConstants clientUIConstants, Rect winRect) {
        int screenWidth = clientUIConstants.getScreenWidth();
        int screenHeight = clientUIConstants.getScreenHeight();
        int statusHeight = clientUIConstants.getStatusHeight();
        int eightOffset = clientUIConstants.getEightOffset();
        int bottomViewHeight = clientUIConstants.getChatBottomViewHeight();

        PositionMeta meta = new PositionMeta();
        meta.setX(screenWidth / 2f);

        int navHeight = 0;
        if (ImmersionBar.isNavigationAtBottom(AICApplication.getCurrentActivity())) {
            navHeight = ImmersionBar.getNavigationBarHeight(AICApplication.getInstance().getApplicationContext());
        }

        if (!winRect.isEmpty()) {
            meta.setY(winRect.bottom - navHeight - bottomViewHeight / 2f);
            return meta;
        }
        meta.setY(screenHeight - navHeight - bottomViewHeight / 2f);
        return meta;
    }

    //在软键盘弹起情况下，通过点击EditText组件获取聊天粘贴菜单位置
    public PositionMeta getChatPasteMenusPositionForEdit(ClientUIConstants clientUIConstants, int softKeyboardHeight, Rect editRect) {
        int screenWidth = clientUIConstants.getScreenWidth();
        int screenHeight = clientUIConstants.getScreenHeight();
        int statusHeight = clientUIConstants.getStatusHeight();
        int bottomViewHeight = clientUIConstants.getChatBottomViewHeight();
        int eightOffset = clientUIConstants.getEightOffset();

        PositionMeta meta = new PositionMeta();
        meta.setX(screenWidth / 2f);

        if (!editRect.isEmpty()) {
            int editHeight = Math.abs(editRect.bottom - editRect.top);
            meta.setY(editRect.bottom - editHeight / 2f);
            return meta;
        }

        int navHeight = 0;
        if (ImmersionBar.isNavigationAtBottom(AICApplication.getCurrentActivity())) {
            navHeight = ImmersionBar.getNavigationBarHeight(AICApplication.getInstance().getApplicationContext());
        }

        meta.setY(screenHeight - navHeight - softKeyboardHeight - bottomViewHeight / 2f + eightOffset);
        return meta;
    }

    //在软键盘弹起情况下，通过点击粘贴菜单的"粘贴"按钮来达到文本赋值的效果
    public PositionMeta getChatEditPasteArea(ClientUIConstants clientUIConstants, int softKeyboardHeight, Rect editRect) {
        int screenHeight = clientUIConstants.getScreenHeight();
        int statusHeight = clientUIConstants.getStatusHeight();
        int pasteMenuWidth = clientUIConstants.getPasteMenuWidth();
        int pasteMenuHeight = clientUIConstants.getPasteMenuHeight();
        int eightOffset = clientUIConstants.getEightOffset();
        int bottomViewHeight = clientUIConstants.getChatBottomViewHeight();

        PositionMeta meta = new PositionMeta();
        meta.setX(eightOffset + pasteMenuWidth / 2f);

        if (!editRect.isEmpty()) {
            meta.setY(editRect.top - pasteMenuHeight / 2f);
            return meta;
        }

        int navHeight = 0;
        if (ImmersionBar.isNavigationAtBottom(AICApplication.getCurrentActivity())) {
            navHeight = ImmersionBar.getNavigationBarHeight(AICApplication.getInstance().getApplicationContext());
        }

        meta.setY(screenHeight - navHeight - softKeyboardHeight - bottomViewHeight + eightOffset - pasteMenuHeight / 2f);
        return meta;
    }

    //获取聊天发送按钮位置
    public PositionMeta getChatSendPosition(ClientUIConstants clientUIConstants, Rect winRect) {
        Context applicationContext = AICApplication.getInstance().getApplicationContext();

        int screenWidth = clientUIConstants.getScreenWidth();
        int screenHeight = clientUIConstants.getScreenHeight();
        int statusHeight = clientUIConstants.getStatusHeight();
        int eightOffset = clientUIConstants.getEightOffset();

        int sendWidth = PixelUtils.dip2px(applicationContext, 54);
        int sendHeight = PixelUtils.dip2px(applicationContext, 28);

        PositionMeta meta = new PositionMeta();
        meta.setX(screenWidth - (sendWidth / 2f) - eightOffset);

        int navHeight = 0;
        if (ImmersionBar.isNavigationAtBottom(AICApplication.getCurrentActivity())) {
            navHeight = ImmersionBar.getNavigationBarHeight(AICApplication.getInstance().getApplicationContext());
        }

        if (!winRect.isEmpty()) {
            meta.setY(winRect.bottom - navHeight - eightOffset - (sendHeight / 2f));
            return meta;
        }

        meta.setY(screenHeight - navHeight - eightOffset - (sendHeight / 2f));
        return meta;
    }
}
